import requests
import json
from faker import Faker

# 初始化Faker实例
fake = Faker('zh_CN')

# 定义性别列表
genders = ["男", "女"]

# 后端API的URL
api_url = "http://127.0.0.1:8000/admin/adduser"

# 发送1000次请求
for _ in range(1):
    # 使用Faker生成假的用户数据，包括性别
    new_user = {
        "name": fake.name(),
        "age": fake.random_int(min=14, max=56),  # 假设年龄在18到99之间
        "gender": fake.random_element(elements=genders)  # 随机选择性别
    }

    # 发送POST请求
    response = requests.post(api_url, data=json.dumps(new_user), headers={'Content-Type': 'application/json'})

    # 打印每次请求的结果状态码
    if response.status_code == 201:
        print(f"User {new_user['name']} added successfully!")
    else:
        print(f"Failed to add user {new_user['name']}. Status code: {response.status_code}")

    # 可选：根据需要暂停以避免请求过快
    # time.sleep(0.1)
